package com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Live.HistoryMeet;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.tnot.R;
import com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Live.HistoryMeet.Adapter.rv_live_historyAdapter;
import com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Live.HistoryMeet.ItemBean.History;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class HistoryMeetFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    View view;
    ImageView back;
    RecyclerView rv_history_meet;
    private EditText et_search;
    private List<History> historyList;
    private List<History> filteredHistoryList;

    public HistoryMeetFragment() {
    }

    public static HistoryMeetFragment newInstance(String param1, String param2) {
        HistoryMeetFragment fragment = new HistoryMeetFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view =  inflater.inflate(R.layout.course_live_history_meet, container, false);
        historyList = new ArrayList<>();
        InitData();

        // 创建过滤后的列表用于显示
        filteredHistoryList = new ArrayList<>(historyList);

        // 设置RecyclerView适配器
        rv_history_meet = view.findViewById(R.id.rv_history_meet);
        rv_live_historyAdapter adapter = new rv_live_historyAdapter(filteredHistoryList);
        rv_history_meet.setAdapter(adapter);
        rv_history_meet.setLayoutManager(new LinearLayoutManager(getActivity()));

        et_search = view.findViewById(R.id.et_search);
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                SearchSomeOne();
            }
        });

        back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();
            }
        });
        return view;
    }

    // 搜索方法
    private void SearchSomeOne() {
        String searchKeyword = et_search.getText().toString().trim().toLowerCase();

        if (searchKeyword.isEmpty()) {
            // 如果搜索框为空，显示所有数据
            filteredHistoryList.clear();
            filteredHistoryList.addAll(historyList);
        } else {
            // 进行搜索过滤
            filteredHistoryList.clear();
            for (History history : historyList) {
                // 检查会议名称是否包含搜索关键词
                if (history.getMeeting_name().toLowerCase().contains(searchKeyword)) {
                    filteredHistoryList.add(history);
                }else if(history.getMeeting_organizer().toLowerCase().contains(searchKeyword)) {
                    filteredHistoryList.add(history);
                }
            }
        }

        // 更新RecyclerView
        rv_live_historyAdapter adapter = (rv_live_historyAdapter) rv_history_meet.getAdapter();
        if (adapter != null) {
            adapter.updateList(filteredHistoryList);
        }
    }
    private void InitData() {
        historyList.add(new History("100801","叶其浩的会议","时间: 08:10","发起人: 叶其浩"));
        historyList.add(new History("100801","我要上课啦!!","时间: 08:10","发起人: 叶其浩"));
        historyList.add(new History("100801","叶其浩","时间: 08:10","发起人: 我"));
        historyList.add(new History("100802","赵京的会议","时间: 08:10","发起人: 赵京"));
        historyList.add(new History("100803","王怡心的会议","时间: 08:10","发起人: 王怡心"));
    }
}